#include "CAnimationManager.h"
#include "CAnimation.h"
#include <fstream>
#include <iostream>

CAnimationManager::CAnimationManager(void)
{

}
CAnimationManager::~CAnimationManager(void)
{

}

CAnimationManager* CAnimationManager::GetInstance()
{
	static CAnimationManager Instance;

	return &Instance;
}
void CAnimationManager::Init()
{

}
void CAnimationManager::Clear()
{

}
void CAnimationManager::Shutdown()
{

}
void CAnimationManager::Load(const char* szFilePath)
{	
	fstream fin(szFilePath, ios::in | ios::binary);
	fin.exceptions(~std::ios_base::goodbit);
	try
	{
		unsigned int byteStrLen;
		fin.read((char*)NumAnim, sizeof(int));
		for(int i = 0; i < NumAnim; ++i)
		{
			//Animation Name
			fin.read((char*)&byteStrLen, sizeof(char));
			fin.clear();
			memset(m_szAniName,0,byteStrLen);
			fin.read(m_szAniName, byteStrLen);
			fin.clear();
			m_szAniName[byteStrLen] = 0;
			char buffer[256];
			sprintf_s(buffer, _countof(buffer), m_szAniName);

			Animate->SetAniName(m_szAniName);

			//Number of Frames
			fin.read((char*)m_nTotalFrames, sizeof(int));

			Animate->SetTotalFrames(m_nTotalFrames);

			//Load in File Path
			fin.read((char*)&byteStrLen, sizeof(char));
			fin.clear();
			memset(m_szImageFile,0,byteStrLen);
			fin.read(m_szImageFile, byteStrLen);
			fin.clear();
			m_szImageFile[byteStrLen] = 0;
			//char buffer[256];
			sprintf_s(buffer, _countof(buffer), "Resource/Animations/%s", m_szImageFile);

			Animate->SetImageFile(m_szImageFile);

			//is it looping
			fin.read((char*)m_bIsLooping, sizeof(bool));

			Animate->SetIsLooping(m_bIsLooping);

			//Trigger Name
			fin.read((char*)&byteStrLen, sizeof(char));
			fin.clear();
			memset(m_szTrigName,0,byteStrLen);
			fin.read(m_szTrigName, byteStrLen);
			fin.clear();
			m_szTrigName[byteStrLen] = 0;
			//char buffer[256];
			sprintf_s(buffer, _countof(buffer), "Resource/Animations/%s", m_szTrigName);

			Animate->SetTrigName(m_szTrigName);

			for(int i = 0; i < m_nTotalFrames; ++i)
			{
				fin.read((char*)m_nFrameCount, sizeof(int));
				fin.read((char*)m_fFrameLength, sizeof(int));
				fin.read((char*)m_rRenderRect.left, sizeof(int));
				fin.read((char*)m_rRenderRect.top, sizeof(int));
				fin.read((char*)m_rRenderRect.right, sizeof(int));
				fin.read((char*)m_rRenderRect.bottom, sizeof(int));
				fin.read((char*)m_rAttackRect.left, sizeof(int));
				fin.read((char*)m_rAttackRect.top, sizeof(int));
				fin.read((char*)m_rAttackRect.right, sizeof(int));
				fin.read((char*)m_rAttackRect.bottom, sizeof(int));
				fin.read((char*)m_rCollisionRect.left, sizeof(int));
				fin.read((char*)m_rCollisionRect.top, sizeof(int));
				fin.read((char*)m_rCollisionRect.right, sizeof(int));
				fin.read((char*)m_rCollisionRect.bottom, sizeof(int));
				fin.read((char*)m_nAnchorPointX, sizeof(int));
				fin.read((char*)m_nAnchorPointY, sizeof(int));
				fin.read((char*)m_nWeaponPointX, sizeof(int));
				fin.read((char*)m_nWeaponPointY, sizeof(int));

				Frame->SetFrameLength(m_fFrameLength);
				Frame->SetRenderRect(m_rRenderRect);
				Frame->SetAttackRect(m_rAttackRect);
				Frame->SetCollisionRect(m_rCollisionRect);
				Frame->SetAnchorPointX(m_nAnchorPointX);
				Frame->SetAnchorPointY(m_nAnchorPointY);
				Frame->SetWeaponPointX(m_nWeaponPointX);
				Frame->SetWeaponPointY(m_nWeaponPointY);
				Frame->SetFrameCount(m_nFrameCount);

				Animate->AddFrame(Frame);
			}
			AddAnimation(Animate);
		}
		fin.close();
	}
	catch(std::ios_base::failure)
	{
		fin.close();
		MessageBox(NULL,
			"File Read Error","Error",MB_OK);
	}

}
void CAnimationManager::Update(float fDelta, Animations* Anim)
{
	Animate->Update(fDelta, Anim);
}
void CAnimationManager::Render(int nPosX, int nPosY, float fScale, DWORD dwColor, Animations* Anim, bool bIsFlipped)
{
	Animate->Render(nPosX, nPosY, fScale, dwColor, Anim, bIsFlipped);
}
void CAnimationManager::AddAnimation(CAnimation* pAnimation)
{
	m_vAnimations.push_back(pAnimation);
}
void CAnimationManager::RemoveAnimation(CAnimation* pAnimation)
{
	
}
